<template>
	<a-card :bordered="false">
		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col :span="6">
					<a-form-item label="流程标题" name="flowTitle">
						<a-input v-model:value="searchFormState.flowTitle" placeholder="请输入流程标题" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="流程类型" name="flowType">
						<a-select
							v-model:value="searchFormState.flowType"
							placeholder="请选择流程类型"
							:options="flowTypeOptions"
						/>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-button type="primary" @click="table.refresh(true)">查询</a-button>
					<a-button style="margin: 0 8px" @click="reset">重置</a-button>
				</a-col>
			</a-row>
		</a-form>
		<s-table
			ref="table"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
			:row-selection="options.rowSelection"
			:loading="loading"
		>
			<template #operator class="table-operator">
				<a-space>
					<a-button type="primary" @click="formRef.onOpen()" v-if="hasPerm('sysFlowConfigAdd')">
						<template #icon><plus-outlined /></template>
						新增
					</a-button>
					<xn-batch-delete
						v-if="hasPerm('sysFlowConfigBatchDelete')"
						:selectedRowKeys="selectedRowKeys"
						@batchDelete="deleteBatchSysFlowConfig"
					/>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'flowType'">
					{{ $TOOL.dictTypeData('FLOW_TYPE', record.flowType) }}
				</template>
				<template v-if="column.dataIndex === 'flowIcon'">
					<component :is="record.flowIcon" />
				</template>
				<template v-if="column.dataIndex === 'flowStatus'">
					<a-switch :checked="record.flowStatus === 1" @change="editStatus(record)" />
				</template>
				<template v-if="column.dataIndex === 'flowVersion'">
					<span>V{{ record.flowVersion }}</span>
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a @click="formRef.onOpen(record)" v-if="hasPerm('sysFlowConfigEdit')">编辑</a>
						<a-divider type="vertical" v-if="hasPerm(['sysFlowConfigEdit', 'sysFlowConfigDelete'], 'and')" />
						<a @click="flowConfig.onOpen(record)" v-if="hasPerm('sysFlowConfigEdit')">配置</a>
						<a-divider type="vertical" v-if="hasPerm(['sysFlowConfigEdit', 'sysFlowConfigDelete'], 'and')" />
						<a @click="deply(record.id)" v-if="hasPerm('sysFlowConfigEdit') && record.flowJson != null">部署</a>
						<a-divider
							type="vertical"
							v-if="hasPerm(['sysFlowConfigEdit', 'sysFlowConfigDelete'], 'and') && record.flowJson != null"
						/>
						<a-popconfirm title="确定要删除吗？" @confirm="deleteSysFlowConfig(record)">
							<a-button type="link" danger size="small" v-if="hasPerm('sysFlowConfigDelete')">删除</a-button>
						</a-popconfirm>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="table.refresh(true)" />
	<config ref="flowConfig" @successful="table.refresh(true)" />
</template>

<script setup name="flowconfig">
	import tool from '@/utils/tool'
	import Form from './form.vue'
	import config from './config.vue'
	import sysFlowConfigApi from '@/api/flow/sysFlowConfigApi'
	import { Modal } from 'ant-design-vue'
	let searchFormState = reactive({})
	const searchFormRef = ref()
	const table = ref()
	const formRef = ref()
	const flowConfig = ref()
	const loading = ref(false)
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	const modalRef = ref(null)
	const columns = [
		{
			title: '流程标题',
			dataIndex: 'flowTitle'
		},
		{
			title: '流程类型',
			dataIndex: 'flowType',
			width: '200px'
		},
		{
			title: '流程图标',
			dataIndex: 'flowIcon',
			align: 'center',
			width: '100px'
		},
		{
			title: '流程版本',
			dataIndex: 'flowVersion',
			width: '100px'
		},
		{
			title: '流程状态',
			dataIndex: 'flowStatus',
			width: '100px'
		},
		{
			title: '流程表单',
			dataIndex: 'formTitle',
			width: '200px'
		},
		{
			title: '创建时间',
			dataIndex: 'createTime',
			width: '150px'
		}
	]
	// 操作栏通过权限判断是否显示
	if (hasPerm(['sysFlowConfigEdit', 'sysFlowConfigDelete'])) {
		columns.push({
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: '250px'
		})
	}
	const selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		// columns数字类型字段加入 needTotal: true 可以勾选自动算账
		alert: {
			show: true,
			clear: () => {
				selectedRowKeys.value = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	const loadData = (parameter) => {
		const searchFormParam = JSON.parse(JSON.stringify(searchFormState))
		return sysFlowConfigApi.sysFlowConfigPage(Object.assign(parameter, searchFormParam)).then((data) => {
			return data
		})
	}
	const editStatus = (record) => {
		loading.value = true
		let status = record.flowStatus == 1 ? 0 : 1
		sysFlowConfigApi
			.sysFlowConfigStatus({ id: record.id, status: status })
			.then(() => {
				table.value.refresh()
			})
			.finally(() => {
				loading.value = false
			})
	}
	const deply = (id) => {
		loading.value = true
		Modal.confirm({
			title: '部署提示',
			content: '部署成功后会覆盖当前流程配置，确认部署吗?',
			maskClosable: false,
			okText: '确定',
			cancelText: '取消',
			onOk: () => {
				sysFlowConfigApi
					.sysFlowDeploy({ id: id })
					.then(() => {
						table.value.refresh()
					})
					.finally(() => {
						loading.value = false
					})
			},
			onCancel: () => {}
		})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		table.value.refresh(true)
	}
	// 删除
	const deleteSysFlowConfig = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		sysFlowConfigApi.sysFlowConfigDelete(params).then(() => {
			table.value.refresh(true)
		})
	}
	// 批量删除
	const deleteBatchSysFlowConfig = (params) => {
		sysFlowConfigApi.sysFlowConfigDelete(params).then(() => {
			table.value.clearRefreshSelected()
		})
	}
	const flowTypeOptions = tool.dictList('FLOW_TYPE')
</script>
